angular.module('app').config(function ($stateProvider) {

    $stateProvider.state('app.inappbrowser', {
        url         : '/inappbrowser',
        label       : 'window.open',
        controller  : 'app.inappbrowser',
        controllerAs: '$ctrl',
        templateUrl : 'view/app/inappbrowser/inappbrowser.html'
    });
}).controller('app.inappbrowser', function ($ionicPlatform) {
    var ctrl = this;
    ctrl.url = 'http://www.baidu.com';
    /**
     * InAppBrowser
     * - addEventListener(eventname, callback): eventname(loadstart 开始加载 url, loadstop url 加载完成, loaderror 加载 Url 时出现一个错误, exit window 关闭)
     * - removeEventListener(eventname, callback):
     * - close():
     * - show():
     * - executeScript(details, callback): details(file 插入的脚本路径, code 插入的脚本字符串)
     * - insertCSS(details, callback): details(file 插入的脚本路径, code 插入的脚本字符串)
     */
    $ionicPlatform.ready(function () {
        ctrl.open = function () {
            /**
             * var ref = cordova.InAppBrowser.open(url, target, options);
             * - ref: InAppBrowser 对象
             * - url: 链接
             * - target: (_self, _blank, _system)
             * - options
             *   - location: 设置成 yes 或 no 来表示是否显示地址栏
             *   [Android only]
             *   - hidden:
             *   - clearcache:
             *   - clearsessioncache:
             *   - zoom:
             *   - hardwareback:
             *   - mediaPlaybackRequiresUserAction:
             *   [iOS only]
             *   - closebuttoncaption:
             *   - disallowoverscroll:
             *   - hidden:
             *   - clearcache:
             *   - clearsessioncache:
             *   - toolbar:
             *   - enableViewportScale:
             *   - mediaPlaybackRequiresUserAction:
             *   - allowInlineMediaPlayback:
             *   - keyboardDisplayRequiresUserAction:
             *   - suppressesIncrementalRendering:
             *   - presentationstyle:
             *   - transitionstyle:
             *   - toolbarposition:
             *   [Windows only]
             *   - hidden:
             *   - fullscreen:
             */
            var ref = cordova.InAppBrowser.open(ctrl.url, '_blank', 'location=yes');
            init(ref);
        };
    });

    function init(InAppBrowser) {
        InAppBrowser.addEventListener('loadstart', function (e) {
            // event => {type, url, code, message}
            alert(JSON.stringify(e));
        });
        InAppBrowser.addEventListener('loadstop', function (e) {
            alert(JSON.stringify(e));
            InAppBrowser.executeScript({
                code: "var evaluateFeedback = function(){return 'Done';};evaluateFeedback();"
            }, function (arr) {
                alert(JSON.stringify(arr));
            });
            InAppBrowser.insertCSS({
                file: 'css/base.css'
            }, function () {
                alert('CSS had injected');
            });
        });
        InAppBrowser.addEventListener('loaderror', function (e) {
            alert(JSON.stringify(e));
        });
        InAppBrowser.addEventListener('exit', function (e) {
            alert(JSON.stringify(e));
        });
        // InAppBrowser.removeEventListener(eventname, callback);
        // InAppBrowser.close();
        // InAppBrowser.show();
    }
});